package com.google.code.jbauble.mvc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class DefaultConfiguration {
	public static final String DEFAULT_CONFIG_FILE = "conf.properties";
	public static final String DEFAULT_VIEW_CONFIG_FILE = "velocity.properties";
	public static final String WEB_PATH = "web.path";
	public static final String WEB_CONF_PATH = "web.conf.path";
	
	public static final String SITE_ENV = "site.env";
	public static final String SITE_ENCODING = "site.encoding";
	
	public static final String MVC_PACKAGE = "mvc.package";
	public static final String MVC_VIEW_DIR = "mvc.view.dir";
	public static final String MVC_VIEW_EXTENSION = "mvc.view.extension";
	public static final String MVC_VIEW_REQUEST = "mvc.view.request";
	public static final String MVC_APPLICATION = "mvc.application";
	public static final String MVC_FILTERS = "mvc.filters";
	public static final String MVC_DEFAULT_LAYOUT = "mvc.default.layout";
	public static final String MVC_CACHE_OVERTIME = "mvc.cache.overtime";
	public static final String MVC_UPLOAD_MAX = "mvc.upload.max";
	
	public static final String SERVICE_SPRING = "service.spring";
	
	private Properties config = null;
	
	public DefaultConfiguration(String file) {
		config = loadConfiguration(file);
	}
	
	public String getProperty(String name) {
		return config.getProperty(name);
	}
	
	private Properties loadConfiguration(String file) {
		Properties p = new Properties();
		try {
			p.load(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			p = null;
			e.printStackTrace();
		} catch (IOException e) {
			p = null;
			e.printStackTrace();
		}
		return p;
	}
	
	public Properties getProperties() {
		return config;
	}
	
	public String getSiteUrl() {
		String env = getProperty(DefaultConfiguration.SITE_ENV);
		return getProperty("site." + env + ".url");
	}
}
